#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>

using namespace std;

// https://leetcode.cn/problems/3sum/?envType=study-plan-v2&envId=top-100-liked
class Solution
{
public:
    vector<vector<int>> threeSum(vector<int> &nums)
    {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        vector<vector<int>> ans;
        for (int i = 0; i < n - 2; i++)
        {
            int left = i + 1;
            int right = n - 1;
            if (i != 0 && nums[i] == nums[i - 1])
            {
                continue;
            }
            if (nums[left] + nums[left + 1] + nums[i] > 0)
            {
                break;
            }
            if (nums[right] + nums[right - 1] + nums[i] < 0)
            {
                continue;
            }

            while (left < right)
            {
                int curSum = nums[left] + nums[right] + nums[i];
                if (curSum < 0)
                {
                    left++;
                    continue;
                }
                if (curSum > 0)
                {
                    right--;
                    continue;
                }
                ans.push_back({nums[i], nums[left], nums[right]});
                for (; left < right && nums[left] == nums[left + 1]; left++)
                    ;
                for (; left < right && nums[right] == nums[right - 1]; right--)
                    ;
                left++;
                right--;
            }
        }
        return ans;
    }
};

int main()
{
    return 0;
}